from DrissionPage import ChromiumPage, ChromiumOptions

import re


co = ChromiumOptions().set_paths(browser_path=r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe") # 设置浏览器路径
co.set_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.2365.52')  # 设置 User-Agent
page = ChromiumPage(co)

# 拿到所有商品链接和商品名称
page.get('https://www.walmart.com/browse/home/decorative-storage/4044_90828_1230055_5137026?povid=Home_POV_HomeManagement_90828StorageOrganization_Campaign_ATF_SpringDecorativeBaskets&seo=home&seo=decorative-storage&seo=4044_90828_1230055_5137026&page=1&affinityOverride=default')


all_ids = []

def get_id(all_ids):
    links = page.eles('x://div[@class="mb0 ph0-xl pt0-xl bb b--near-white w-25 pb3-m ph1"]')
    # print(links)

    # 遍历链接，获取属性
    for link in links:
        # 通过 link-identifier 属性来定位元素
        link_identifier = link.ele('x://a[@class="w-100 h-100 z-1 hide-sibling-opacity  absolute"]')
        link_identifier = str(link_identifier)
        # print(f"link-identifier: {link_identifier}")

        # 使用正则表达式提取 link-identifier 的值
        match = re.search(r"link-identifier='(\d+)'", link_identifier)

        if match:
            extracted_id = match.group(1)
            # print(f"提取到的 link-identifier: {extracted_id}")
            all_ids.append(extracted_id)
        else:
            print("没有找到 link-identifier 属性。")

    return all_ids


while True:
    all_ids = get_id(all_ids)
    page.wait(2)

    next_page = page.ele('x://i[@class="ld ld-ChevronRight pv1 primary"]')
    if next_page:
        next_page.click()
        print("点击下一页")
        page.wait(2)  # 增加等待时间，确保新页面加载完成

    else:
        print("没有下一页了")
        break

all_ids = list(set(all_ids))

# 保存为txt D:\subtitle\goods\all_ids.txt
with open(r'D:\subtitle\goods\all_ids.txt', 'w') as f:
    for id in all_ids:
        f.write(str(id) + '\n')